package slxy.jiangtao.dts.controller.admin;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.entity.sys.Menu;

/**
 * Code must be sexy
 * Created by JiangTao on 2017/1/20.
 */

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    CommonService commonService;

    /**
     * 菜单首页跳转
     */
    @RequestMapping("index")
    public String index(ModelMap modelMap) {
        modelMap.put("menuList", JSON.toJSON(commonService.getList(Menu.class)));
        return "systemmanager/menu";
    }

    @RequestMapping("edit")
    @ResponseBody
    public JSONResult edit(@RequestParam Integer id,@RequestParam String name){
        Menu menu = commonService.get(Menu.class,id);
        menu.setName(name);
        commonService.save(menu);
        return JSONResult.success();
    }


    /**
     * 获取所有菜单
     */
    @RequestMapping(value = "getmenus",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getmenus() {
        return JSONResult.success(commonService.getList(Menu.class));
    }

    /**
     * 跳转编辑页面edit
     */
    @RequestMapping("editmenu")
    public String edit(ModelMap modelMap, @RequestParam Integer id) {
        modelMap.put("menu", commonService.get(Menu.class, id));
        return "admin/editMenu";
    }

    /**
     * 提交菜单更新
     */
    @RequestMapping(value = "editmenuSubmint", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult editmenuSubmint(Menu menu) {
        try {
            commonService.updateEntity(menu);
        } catch (Exception e) {
            return JSONResult.failure("修改失败");
        }
        return JSONResult.success();
    }

    @RequestMapping(value = "addmenu", method = RequestMethod.GET)
    public String addmenuGet(ModelMap modelMap, Integer id) {
        //此处获得的是父级菜单
        Menu menu = commonService.get(Menu.class, id);
        //根据父菜单设置子菜单信息
        Menu childmenu = new Menu();
        childmenu.setLevel(menu.getLevel() + 1);
        childmenu.setParentId(menu.getId());
        if (menu.getIcon().equals("home")) {
            childmenu.setIcon("table");
        } else if (menu.getIcon().equals("table")) {
            childmenu.setIcon("list");
        } else {
            childmenu.setIcon("list");
        }
        modelMap.addAttribute("childmenu", childmenu);
        return "admin/addMenu";
    }

    /**
     * 新增菜单提交
     */
    @RequestMapping(value = "addmenu", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult addmenuPost(Menu menu) {
        return (int) commonService.save(menu) > 0 ? JSONResult.success("添加菜单成功") : JSONResult.failure("添加菜单失败");
    }


}
